package com.lmt.org.controller;

import com.lmt.basic.utils.AjaxResult;
import com.lmt.basic.utils.BaseConstant;
import com.lmt.basic.utils.PageList;
import com.lmt.org.domain.Department;
import com.lmt.org.query.DepartmentQuery;
import com.lmt.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 部门信息CRUD
 * @Author: Amazing
 * @Date: 2022/6/24 20:15
 * @Version: 1.0
 */
@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
    * @Title: loadById
    * @Description: 通过id查询部门信息
    * @Author: Amazing
    * @Version: 1.0
    * @Date:  2022/6/24 20:20
    * @Parameters: [id]
    * @Return com.lmt.org.domain.Department
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department loadById(@ApiParam(name="ID属性",required = true)  @PathVariable("id") Long id){
           return departmentService.loadById(id);
    }
    /**
    * @Title: loadAll
    * @Description: 查询所有部门信息
    * @Author: Amazing
    * @Version: 1.0
    * @Date:  2022/6/24 20:24
    * @Parameters: []
    * @Return java.util.List<com.lmt.org.domain.Department>
    */
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Department> loadAll() {
        return departmentService.loadAll();
    }

    /**
    * @Title: remove
    * @Description: 删除部门信息
    * @Author: Amazing
    * @Version: 1.0
    * @Date:  2022/6/24 20:31
    * @Parameters: [id]
    * @Return com.lmt.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除" )
    public AjaxResult remove(@ApiParam(name="ID属性",required = true)  @PathVariable("id") Long id) {
        try {
            departmentService.remove(id);
            return AjaxResult.getInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getInstance().setSuccess(false).setMessage(BaseConstant.AJAX_RESULT_MESSAGE);
        }
    }
    /**
    * @Title: addOrUpt
    * @Description: 新增或者修改
    * @Author: Amazing
    * @Version: 1.0
    * @Date:  2022/6/24 20:38
    * @Parameters: [department]
    * @Return com.lmt.basic.utils.AjaxResult
    */
    @PutMapping
    @ApiOperation(value = "部门信息新增或者修改" )
    public AjaxResult addOrUpt(@ApiParam(name="部门详细信息",required = true)  @RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.getInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getInstance().setSuccess(false).setMessage(BaseConstant.AJAX_RESULT_MESSAGE);
        }
    }

    /**
    * @Title: queryData
    * @Description: 分页+高级查询
    * @Author: Amazing
    * @Version: 1.0
    * @Date:  2022/6/24 20:50
    * @Parameters: [departmentQuery]
    * @Return com.lmt.basic.utils.PageList<com.lmt.org.domain.Department>
    */
    @PostMapping
    @ApiOperation(value = "分页+高级查询" )
    public PageList<Department> queryData(@ApiParam(name="当前页和每页展示条数",required = true) @RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryData(departmentQuery);
    }

}
